package com.example.customizedgroupmessage;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private ArrayList<Custom> items;
	private Context context;
	private int layout;
	SQLiteDatabase db;
	DBHelper mHelper;	
	private EditText specialMsg;
	private TextView specialByte;
	private final int LIMIT=12;
	public CustomAdapter(Context _context, int _layout, ArrayList<Custom> _items) {
		//super(context, items);
		//this.context = _context;
		//this.layout = _layout;
		//this.items = _items;
		context = _context;
		layout = _layout;
		items = _items;
		inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		  return items.size();
	}
	@Override
	public Object getItem(int position) {
		  return items.get(position);
	}
	@Override
	public long getItemId(int position) {
		  return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
				final int pos = position;
			
				View convertView02=convertView;
				int res =R.layout.sms_word;
			    int res01=R.layout.sms_custom_word;
			    
				convertView =inflater.inflate(res,parent, false); //smsword 레이아웃 customname과 customword는 이안에 있음 
				convertView02=inflater.inflate(res01, parent, false); //smscustomword 레이아웃 btnNext은 그 안에 있음

				
				TextView name =(TextView)convertView.findViewById(R.id.customName);
				final String strName =items.get(position).getName();
				name.setText(strName);
				
				TextView orgAndGroup =(TextView)convertView.findViewById(R.id.orgAndgroup);
				
				String strOrg =items.get(position).getOrganization();
				String strGroup =items.get(position).getGroupName();
				
				if(strOrg!=null){				
					orgAndGroup.setText(strOrg);}
				else if(!strGroup.equals("")){
					orgAndGroup.setText(strGroup);}
				else {
					orgAndGroup.setText("소속그룹 없음");
					}
				
			
				
				
				Button btnEdit = (Button)convertView.findViewById(R.id.btnEdit);
				btnEdit.setFocusable(false);
				
				btnEdit.setOnClickListener(
						new Button.OnClickListener() {
							@Override
							public void onClick(View v) {  
								View layout = View.inflate(context, R.layout.special_edit_dialog, null);  
								specialMsg = (EditText) layout.findViewById(R.id.specialEdit);
								//specialByte = (TextView) layout.findViewById(R.id.specialByte);
								 AlertDialog dialog;
				 
								 
								Builder builder = new AlertDialog.Builder(context);
								builder.setTitle(strName+"의 Special Msg 작성");
								builder.setView(layout);
								dialog=builder.create();
								

								/*specialMsg.addTextChangedListener(new TextWatcher() {
									String beforeText;

									@Override
									public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
										beforeText = charSequence.toString();
									}

									@Override
									public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

									}

									@Override
									public void afterTextChanged(Editable editable) {
										int length = showBytes();
										if(length > LIMIT) specialMsg.setText(beforeText);
									}
								});*/
								
								builder.setNegativeButton("저장", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int whichButton) {
										String custom = specialMsg.getText().toString();
										//Toast.makeText(context, custom, Toast.LENGTH_SHORT).show();
										mHelper = new DBHelper(context);
										db = mHelper.getWritableDatabase();
										String id = items.get(pos).getId();

										db.execSQL("UPDATE sms SET custom ='"+custom+"' WHERE _id = '" + id + "';");

										mHelper.close();

										if(specialMsg.getText().toString().length()==0){
											Toast.makeText(context, strName+"빈칸입니다.", Toast.LENGTH_SHORT).show();
										
										}
										else Toast.makeText(context, strName+"의 내용이저장되었습니다.", Toast.LENGTH_SHORT).show();
									}
								});
								//showBytes();
								builder.show();
							}
						}
						);
				
				
				Button btnNext = (Button)convertView02.findViewById(R.id.btnNext);
				
		return convertView;		
	}

	protected void getApplicationContext() {
		// TODO Auto-generated method stub
		
	}
	/*private int showBytes(){
		try {
			int length = specialMsg.getText().toString().getBytes("EUC-KR").length;
			specialByte.setText(length+" / 12bytes");
			return length;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return -1;
	}*/

}